﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Reflection;
using log4net;
using System.Security.Policy;

namespace ZuggerWpf
{
    class GetBug : ActionBase, IActionBase
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        ZuggerObservableCollection<BugItem> itemsList = null;

        public GetBug(ZuggerObservableCollection<BugItem> zItems)
        {
            itemsList = zItems;
        }

        public override bool Action()
        {
            bool isSuccess = false;
            NewItemCount = 0;

            ItemCollectionBackup.AddRange(itemsList.Select(f => f.ID));
            itemsList.Clear();

            ApplicationConfig appconfig = IOHelper.LoadIsolatedData();

            for (int i = 0; i < Products.Length; i++) {
                JObject header = new JObject { { "Token", Token } };
                JObject res = WebTools.Get(appconfig.GetBugUrl.Replace("{ID}", Products[i]), header);
                
                JArray bugs = (JArray)res["bugs"];
                foreach (var j in bugs)
                {
                    string openby_account = string.Empty;
                    string assignedto_account = string.Empty;

                    openby_account = j["openedBy"]["account"].Value<string>();
                    try
                    {
                        JObject assignedto = (JObject)j["assignedTo"];
                        if (assignedto != null)
                        {
                            assignedto_account = assignedto["account"].Value<string>();
                        }
                    }
                    catch { 
                    }
                   
                    if (openby_account != appconfig.UserName && assignedto_account != appconfig.UserName) {
                        continue;
                    }

                    BugItem bi = new BugItem()
                    {
                        priority = j["pri"].Value<string>()
                        ,
                        Severity = j["severity"].Value<string>()
                        ,
                        ID = j["id"].Value<int>()
                        ,
                        Title = Util.EscapeXmlTag(j["title"].Value<string>())
                        ,
                        OpenDate = j["openedDate"].Value<string>()
                        ,
                        LastEdit = j["lastEditedDate"].Value<string>()
                        ,
                        Tip = "Bug"
                    };

                    //防止报错
                    bi.LastEdit = bi.LastEdit != null ? bi.LastEdit : bi.OpenDate;

                    if (!ItemCollectionBackup.Contains(bi.ID))
                    {
                        NewItemCount = NewItemCount == 0 ? bi.ID : (NewItemCount > 0 ? -2 : NewItemCount - 1);
                    }

                    itemsList.Add(bi);
                }
            }

            if (OnNewItemArrive != null && NewItemCount != 0)
            {
                OnNewItemArrive(ItemType.Bug, NewItemCount);
            }
            ItemCollectionBackup.Clear();
            return isSuccess;
        }

        #region ActionBaseInterface Members

        public event NewItemArrive OnNewItemArrive;

        #endregion
    }
}
